package com.fang.system.io.mocknetty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/15 3:24 下午
 **/
public class ClientReader implements Handler {
    private SocketChannel client;

    public ClientReader(SocketChannel client) {
        this.client = client;
    }

    @Override
    public void doRead() {
        ByteBuffer buffer = ByteBuffer.allocateDirect(4096);
        try{
            client.read(buffer);
            buffer.flip();
            byte[] data = new byte[buffer.limit()];
            buffer.get(data);
            System.out.println(new String(data));
            buffer.clear();
            // 回写到客户端
            buffer.put("已接收".getBytes());
            buffer.flip();
            client.write(buffer);
            buffer.clear();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
